package com.example.downloaddemo;

import android.os.Handler;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by xiewei on 15-2-4.
 */
public class ListThread extends Thread {
	private String urlstr;
	private Handler handler;
	private List<String> urllist;
	public ListThread(String url, List<String> urllist, Handler handler){
		this.urlstr=url;
		this.urllist=urllist;
		this.handler=handler;
	}
	@Override
	public void run() {
		try {
			URL url=new URL(urlstr);
			HttpURLConnection conn= (HttpURLConnection) url.openConnection();
			InputStream in=conn.getInputStream();
			byte[] data = new byte[2048];
			int len = 0;
			StringBuffer stb=new StringBuffer();
			while ((len = in.read(data)) != -1) {
				stb.append(new String(data,0,len));
			}
			Pattern p = Pattern.compile("http://[a-zA-Z0-9./_]*\\.apk");
			Matcher m = p.matcher(stb);
			while (m.find()) {
				String str = m.group();
				urllist.add(str);
				//Log.i("msg",urllist.size()+"aa");

			}
			handler.sendEmptyMessage(0);
		} catch (Exception e) {
			handler.sendEmptyMessage(-1);
			e.printStackTrace();
		}

	}
}
